package com.yixun.qfbao.api.service.impl;

import com.yixun.qfbao.api.service.PremisesRecommenderService;
import com.yixun.qfbao.apidto.PremisesRecommenderDto;
import com.yixun.qfbao.apidto.SyncIntentionRecommenderReqDto;
import com.yixun.qfbao.mapper.PremisesRecommenderMapper;
import com.yixun.qfbao.xf.model.PremisesRecommender;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @program: channel-parent
 *
 * @description: 楼盘推荐人
 *
 * @author: Seven.wang
 *
 * @create: 2019-11-05 11:34
 **/
@Service("premisesRecommenderService")
public class PremisesRecommenderServiceImpl implements PremisesRecommenderService {

    private static final Logger logger = LoggerFactory.getLogger(PremisesRecommenderServiceImpl.class);

    @Autowired
    PremisesRecommenderMapper premisesRecommenderMapper;

    @Override
    public List<PremisesRecommenderDto> selectListByPremisesId(Long premisesId) {
        List<com.yixun.qfbao.model.dto.PremisesRecommenderDto> dtoList = premisesRecommenderMapper.selectListByPremisesId(premisesId);
        List<PremisesRecommenderDto> resDtoList = dtoList.stream().map(temp -> {
            PremisesRecommenderDto obj = new PremisesRecommenderDto();
            if(StringUtils.isNotBlank(temp.getParentDeptName())){
                obj.setDeptName(temp.getParentDeptName()+"-"+temp.getDeptName());
            }else{
                obj.setDeptName(temp.getDeptName());
            }
            obj.setPhone(temp.getPhone());
            obj.setUserName(temp.getUserName());
            if("0".equals(temp.getAccountType())){
                obj.setRoleName("超级管理员");
            }else{
                obj.setRoleName(temp.getRoleName());
            }
            obj.setStatus(temp.getStatus());
            obj.setRecommenderUuid(temp.getRecommenderUuid());
            obj.setRecommenderId(temp.getRecommenderId());
            return obj;
        }).collect(Collectors.toList());
        return resDtoList;
    }
    
    @Override
    public Map<String, List<PremisesRecommenderDto>> selectListByPremisesIds(List<Long> premisesIds) {
    	List<com.yixun.qfbao.model.dto.PremisesRecommenderDto> dtoList = premisesRecommenderMapper.selectListByPremisesIds(premisesIds);
    	Map<String, List<PremisesRecommenderDto>> premisesRecommenderMap = new HashMap<>();
    	dtoList.stream().forEach(premisesRecommender -> {
    		Long premisesId = premisesRecommender.getPremisesId();
    		List<PremisesRecommenderDto> premisesRecommenderDtoList = 
    				premisesRecommenderMap.get(String.valueOf(premisesId));
    		if(CollectionUtils.isEmpty(premisesRecommenderDtoList)) {
    			premisesRecommenderDtoList = new ArrayList<>();
    			premisesRecommenderMap.put(String.valueOf(premisesId), premisesRecommenderDtoList);
    		}
    		PremisesRecommenderDto premisesRecommenderDto = new PremisesRecommenderDto();
    		premisesRecommenderDto.setRecommenderUuid(premisesRecommender.getRecommenderUuid());
    		premisesRecommenderDto.setUserName(premisesRecommender.getUserName());
    		premisesRecommenderDtoList.add(premisesRecommenderDto);
    	});
        return premisesRecommenderMap;
    }

    @Override
    public List<PremisesRecommenderDto> selectListForPremisesInfo(Long premisesId) {
        List<com.yixun.qfbao.model.dto.PremisesRecommenderDto> dtoList = premisesRecommenderMapper.selectListForPremisesInfo(premisesId);
        List<PremisesRecommenderDto> resDtoList = dtoList.stream().map(temp -> {
            PremisesRecommenderDto obj = new PremisesRecommenderDto();
            if(StringUtils.isNotBlank(temp.getParentDeptName())){
                obj.setDeptName(temp.getParentDeptName()+"-"+temp.getDeptName());
            }else{
                obj.setDeptName(temp.getDeptName());
            }
            obj.setPhone(temp.getPhone());
            obj.setUserName(temp.getUserName());
            obj.setRecommenderUuid(temp.getRecommenderUuid());
            obj.setRecommenderId(temp.getRecommenderId());
            return obj;
        }).collect(Collectors.toList());
        return resDtoList;
    }

    @Override
    public List<SyncIntentionRecommenderReqDto> selectListForIntentionRecommender(Long premisesId) {
        List<com.yixun.qfbao.model.dto.PremisesRecommenderDto> dtoList = premisesRecommenderMapper.selectListForPremisesInfo(premisesId);
        List<SyncIntentionRecommenderReqDto> reqDtoList = dtoList.stream().map(temp -> {
            SyncIntentionRecommenderReqDto obj = new SyncIntentionRecommenderReqDto();
            if(StringUtils.isNotBlank(temp.getParentDeptName())){
                obj.setRecommenderDeptName(temp.getParentDeptName()+"-"+temp.getDeptName());
            }else{
                obj.setRecommenderDeptName(temp.getDeptName());
            }
            obj.setRecommenderEmployeeUuid(temp.getRecommenderUuid());
            obj.setRecommenderEmployeeName(temp.getUserName());
            return obj;
        }).collect(Collectors.toList());
        return reqDtoList;
    }

    @Override
    public void batchDeleted(List<PremisesRecommender> list) {
        premisesRecommenderMapper.batchDeleted(list);
    }

    @Override
    public void batchAdd(List<PremisesRecommender> list) {
        premisesRecommenderMapper.batchAdd(list);
    }

    @Override
    public PremisesRecommenderDto getPremisesRecommenderByUuid(String recommenderUuid) {
        return convertPremisesRecommenderDto(recommenderUuid);
    }

    @Override
    public List<PremisesRecommender> selectListByIds(List<Long> premisesIds, List<Long> recommenderIds, String deleteFlag) {
        return premisesRecommenderMapper.selectListByIds(premisesIds, recommenderIds, deleteFlag);
    }

    @Override
    public List<PremisesRecommender> selectPremisesRecommenderByPremisesId(Long premisesId) {
        return premisesRecommenderMapper.selectPremisesRecommenderByPremisesId(premisesId);
    }

    /** PremisesRecommenderDto 转化 */
    private PremisesRecommenderDto convertPremisesRecommenderDto(String recommenderUuid){
        com.yixun.qfbao.model.dto.PremisesRecommenderDto entityDto = premisesRecommenderMapper.getPremisesRecommenderByUuid(recommenderUuid);
        PremisesRecommenderDto dto = new PremisesRecommenderDto();
        // 避免报备推荐人被取消而造成的空指针
        if(entityDto == null) {
        	return dto;
        }
        dto.setRecommenderUuid(entityDto.getRecommenderUuid());
        if(StringUtils.isNotEmpty(entityDto.getParentDeptName())){
            dto.setDeptName(entityDto.getParentDeptName()+"-"+entityDto.getDeptName());
        }else{
            dto.setDeptName(entityDto.getDeptName());
        }
        dto.setPhone(entityDto.getPhone());
        dto.setUserName(entityDto.getUserName());
        dto.setStatus(entityDto.getStatus());
        dto.setRecommenderId(entityDto.getRecommenderId());
        return dto;
    }

    @Override
    public List<PremisesRecommenderDto> selectPremisesRecommenderList(Long companyId) {
        List<com.yixun.qfbao.model.dto.PremisesRecommenderDto> dtoList = premisesRecommenderMapper.selectPremisesRecommenderList(companyId);
        List<PremisesRecommenderDto> resDtoList = dtoList.stream().map(temp -> {
            PremisesRecommenderDto obj = new PremisesRecommenderDto();
            obj.setUserName(temp.getUserName());
            obj.setRecommenderUuid(temp.getRecommenderUuid());
            obj.setRecommenderId(temp.getRecommenderId());
            return obj;
        }).collect(Collectors.toList());
        return resDtoList;
    }

    @Override
    public List<PremisesRecommenderDto> selectRecommenderList(Long companyId) {
        List<com.yixun.qfbao.model.dto.PremisesRecommenderDto> dtoList = premisesRecommenderMapper.selectRecommenderList(companyId);
        List<PremisesRecommenderDto> resDtoList = dtoList.stream().map(temp -> {
            PremisesRecommenderDto obj = new PremisesRecommenderDto();
            //查询所有推荐人现在是否仍为推荐人,若现在不是任何楼盘的推荐人则加上(历史)
            List<com.yixun.qfbao.model.dto.PremisesRecommenderDto> dtos = premisesRecommenderMapper.selectListById(temp.getRecommenderId());
            if (dtos == null || dtos.size() == 0) {
                obj.setUserName(temp.getUserName() + " (历史)");
            } else {
                obj.setUserName(temp.getUserName());
            }
            obj.setRecommenderUuid(temp.getRecommenderUuid());
            obj.setRecommenderId(temp.getRecommenderId());
            return obj;
        }).collect(Collectors.toList());
        return resDtoList;
    }

    @Override
    public List<PremisesRecommender> selectPremisesRecommenderById(Long recommenderId) {
        List<Long> recommenderIds = new ArrayList<>();
        recommenderIds.add(recommenderId);
        return premisesRecommenderMapper.selectListByIds(null, recommenderIds, null);
    }
}
